package com.kunbo.cn.service.impl.core.purchase;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import com.kunbo.cn.dao.core.purchase.PurchaseOrderQuantityAdjustDao;
import com.kunbo.cn.entity.core.purchase.PurchaseOrderQuantityAdjust;
import com.kunbo.cn.service.core.purchase.PurchaseOrderQuantityAdjustService;
import com.kunbo.cn.util.date.SDate;
import com.kunbo.cn.util.retu.Pages;
/**
 *
 * @author bjl  
 * @date 2019年4月18日 
 */
@Service
public class PurchaseOrderQuantityAdjustServiceImpl implements  PurchaseOrderQuantityAdjustService {
	@Autowired
	private PurchaseOrderQuantityAdjustDao dao;
	@Autowired
    private JdbcTemplate jdbcTemplate;
	
	@Override
	public void save(PurchaseOrderQuantityAdjust soqa) {
		soqa.setCreate_time(SDate.getSystemDateToString());
		dao.save(soqa);
	}

	@Override
	public List<Map<String, Object>> list(String order_code) {
		String sql = "SELECT * FROM business_purchase_order_quantity_adjust p where 1=1 and p.purchase_order_code='"+order_code+"'  order by p.create_time desc";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		return list;
	}
	@Override
	public List<Map<String, Object>> OrderList() {
		String sql = "SELECT p.purchase_order_code,p.purchase_waybill_placeoftransportation,p.material_type,p.material_name,p.number_of_contracts,p.actual_quantity,"
				+ "p.allowance,p.purchasing_unit,p.product_name FROM bussiness_purchasing_order_manager p where 1=1" ; 
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);//and p.state <> 5 and p.state <> 6
		return list;
	}
	@Override
	public Pages OrderList1(Integer page ,Integer pageSize) {
		String sql1 = "SELECT p.purchase_order_code,p.purchase_waybill_placeoftransportation,p.material_type,p.material_name,p.number_of_contracts,p.actual_quantity,"
				+ "p.allowance,p.purchasing_unit,p.product_name FROM bussiness_purchasing_order_manager p where 1=1" ; 
		  String sql2="";
		sql2+= " order by id desc limit " + (page*pageSize) + "," + pageSize;
		  String sql=sql1.concat(sql2);//获取数据sql
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);//and p.state <> 5 and p.state <> 6
		Pages pages =new Pages();
		  Integer count = jdbcTemplate.queryForList(sql1).size();
		  pages.setTotal(count);
		  pages.setData(list);
		return pages;
	}
	@Override
	public int AdjustOrderNum( String purchase_order_code,String number_of_contracts,String actual_quantity,String adjust_amount, String allowance ) throws SQLException {
		int n=0; int i=0;int n1=0;
		  //if((sale_order_code!=null && !adjust_amount.isEmpty())&&(newOrder!=null && !newOrder.isEmpty())) {
		Float number_of_contractss =Float.valueOf(number_of_contracts);
		Float actual_quantitys =Float.valueOf(actual_quantity);
		Float adjust_amounts =Float.valueOf(adjust_amount);
		Float allowances =Float.valueOf(allowance);
		Float new_number_of_contracts=number_of_contractss+adjust_amounts;
		Float new_actual_quantity=actual_quantitys+adjust_amounts;
		Float new_allowance= allowances+adjust_amounts;
		if((purchase_order_code!=null && !adjust_amount.isEmpty())){
		//1.修改调量订单(先查询余量)
			String sql = "UPDATE \r\n" +
	        "bussiness_purchasing_order_manager p \r\n"+
	        " set p.allowance="+new_allowance+"\r\n "+
	        "  ,p.actual_quantity="+new_actual_quantity+"\r\n "+
	        "  ,p.number_of_contracts="+new_number_of_contracts+"\r\n "+
	        " WHERE  p.purchase_order_code='"+purchase_order_code+"'";
			 i=jdbcTemplate.update(sql);
	}
		return i;
	}
}
